﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using InspectionMAC.Config;
using csLTDMC;
using InspectionMAC.Function;
using InspectionMAC.Algorithm;

namespace InspectionMAC.SystemThread
{
    class CameraTriggerThread
    {
        public static bool CameraTrigger_Flowrec = false;
        public static Thread CameraTriggerFlow_Thread = null;
        public static int iCameraTrigger_Step = 0;

        public static bool CameraTrigger_Stop = false;

        public static DateTime triggerDataTime;
        public static void CameraTrigger_Thread()
        {
            CameraTrigger_Flowrec = true;
            CameraTriggerFlow_Thread = new Thread(CameraTriggerFlowThread);
            CameraTriggerFlow_Thread.IsBackground = true;
            CameraTriggerFlow_Thread.Start();
            Console.WriteLine("iCameraTrigger_Step: " + iCameraTrigger_Step);
        }

        private static void CameraTriggerFlowThread(object obj)
        {
            while (CameraTrigger_Flowrec)
            {
                switch (iCameraTrigger_Step)
                {
                    case 0:
                        {
                            break;
                        }
                    case 10://等待入料  光纤一触发
                        {
                            //if (DIO_Thread.Di[11] == 1 && !CameraTriggerTime_Config.CamearTriggerPause01) //DI值需要替换
                            //{
                            //    triggerDataTime = DateTime.Now;
                            //    iCameraTrigger_Step = 15;
                            //}
                            if (SensorStateConfig.isTrigger01 && !CameraTriggerTime_Config.CamearTriggerPause01)
                            {
                                    triggerDataTime = DateTime.Now;
                                    iCameraTrigger_Step = 15;
                            }
                            //停止触发流程
                            if (DMC300_Info.CameraTrigger_IsStop01)
                            {
                                iCameraTrigger_Step = 0;
                            }
                            break;
                        }
                    case 11:
                        {
                            break;
                        }
                    case 12:
                        {
                            break;
                        }
                    case 13:
                        {
                            break;
                        }
                    case 14:
                        {
                            break;
                        }
                    case 15://等待固定距离后
                        {
                            //比较点
                            ComparePoints();
                            bool isDuration = false;
                            isDuration = true;
                            if (isDuration)
                            {
                                iCameraTrigger_Step = 20;
                            }


                            break;
                        }
                    case 20://触发第一站拍照  +  检测+记录结果
                        {
                            DIO_Thread.Do[1] = 1;//DO值需要替换
                            iCameraTrigger_Step = 21;
                            break;
                        }
                    case 21://算法
                        {
                            Console.WriteLine("图片数据长度: " + MainForm.main_Form.m_stFrameInfo[0].nFrameLen);

                            //判断超时NG
                            DateTime imgDateTime = DateTime.Now;
                            Console.WriteLine("triggerDataTime：{0} -imageDateTime：{1} -timeSpan：{2} ", triggerDataTime.TimeOfDay, imgDateTime.TimeOfDay, (imgDateTime - triggerDataTime).Milliseconds);
                            if ((imgDateTime - triggerDataTime).Milliseconds >= 150)
                            {
                                Console.WriteLine("超时NG");
                            }

                            //算法信息
                            AlgorithmModule.ImageHandle();

                            UInt32 nCmdPos = 0; //指令位置
                            UInt32 nEnPos = 0; //编码器反馈位置
                            nCmdPos = LTDMC.dmc_get_position(DMC300_Info.usCardId[0], 0);
                            nEnPos = LTDMC.dmc_get_encoder(DMC300_Info.usCardId[0], 0);

                            //记录1号相机-时间-位置信息（脉波变量）-图片信息（图片宽/高/名称）-瑕疵信息-NG信息
                            CameraResult01 cameraResult01 = new CameraResult01
                            {
                                DataTime = DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss:ffff"),
                                Location = "",
                                iWidth = "",
                                iHeight = "",
                                iName = "",
                                Faults = "",
                                isNG = false,
                                isOkSave = false
                            };
                            Console.WriteLine("CameraResult01: "+cameraResult01.DataTime);

                            MainFlowInfos.infoID += 1;

                            Guid guid = Guid.NewGuid();
                            Console.WriteLine(guid);
                            SingleItemInfo singleItemInfo = new SingleItemInfo
                            {
                                ID = guid.ToString(),
                                Name = "",
                                CameraResult01 = new CameraResult01(),
                                CameraResult02 = new CameraResult02(),
                                CameraResult03 = new CameraResult03()
                            }; 
                            SingleItemInfo singleItemInfo1 = new SingleItemInfo
                            {
                                ID = guid.ToString(),
                                Name = "",
                                CameraResult01 = new CameraResult01(),
                                CameraResult02 = new CameraResult02(),
                                CameraResult03 = new CameraResult03()
                            };
                            MainFlowInfos.singleItemInfos.Add(singleItemInfo);
                            MainFlowInfos.singleItemInfos[0] = singleItemInfo1;
                            singleItemInfo.CameraResult01 = cameraResult01;
                            iCameraTrigger_Step = 25;
                            break;
                        }
                    case 25://输出讯号持续时间
                        {
                            Thread.Sleep(DMC300_Info.CameraTrigger_Time_01);
                            iCameraTrigger_Step = 30;
                            break;
                        }
                    case 30://关闭输出讯号  
                        {
                            DIO_Thread.Do[1] = 0;//DO值需要替换
                            iCameraTrigger_Step = 35;
                            break;
                        }
                    case 35://等待固定距离后
                        {
                            //比较点
                            ComparePoints();
                            bool isDuration02 = false;
                            isDuration02 = true;
                            if (isDuration02)
                            {
                                iCameraTrigger_Step = 40;
                            }

                            break;
                        }
                    case 40://第二站拍照  +  检测+记录结果
                        {
                            DIO_Thread.Do[2] = 1;//DO值需要替换
                            iCameraTrigger_Step = 41;
                            break;
                        }
                    case 41://算法
                        {
                            Console.WriteLine("图片数据长度: " + MainForm.main_Form.m_stFrameInfo[0].nFrameLen);

                            //判断超时NG
                            DateTime imgDateTime = DateTime.Now;
                            Console.WriteLine("triggerDataTime：{0} -imageDateTime：{1} -timeSpan：{2} ", triggerDataTime.TimeOfDay, imgDateTime.TimeOfDay, (imgDateTime - triggerDataTime).Milliseconds);
                            if ((imgDateTime - triggerDataTime).Milliseconds >= 150)
                            {
                                Console.WriteLine("超时NG");
                            }

                            //算法信息
                            AlgorithmModule.ImageHandle();

                            UInt32 nCmdPos = 0; //指令位置
                            UInt32 nEnPos = 0; //编码器反馈位置
                            nCmdPos = LTDMC.dmc_get_position(DMC300_Info.usCardId[0], 0);
                            nEnPos = LTDMC.dmc_get_encoder(DMC300_Info.usCardId[0], 0);

                            //记录2号相机-时间-位置信息（脉波变量）-图片信息（图片宽/高/名称）-瑕疵信息-NG信息
                            CameraResult02 cameraResult02 = new CameraResult02
                            {
                                DataTime = DateTime.Now.ToString("yyyy:MM:dd:HH:mm:ss:ffff"),
                                Location = "",
                                iWidth = "",
                                iHeight = "",
                                iName = "",
                                Faults = "",
                                isNG = false,
                                isOkSave = false
                            };
                            Console.WriteLine("CameraResult02: " + cameraResult02.DataTime);

                            iCameraTrigger_Step = 45;
                            break;
                        }
                    case 45://输出讯号持续时间
                        {
                            Thread.Sleep(DMC300_Info.CameraTrigger_Time_02);
                            iCameraTrigger_Step = 50;
                            break;
                        }
                    case 50://关闭输出讯号
                        {
                            DIO_Thread.Do[2] = 0;//DO值需要替换
                            iCameraTrigger_Step = 55;
                            break;
                        }
                    case 55:
                        {
                            if (DIO_Thread.Di[11] == 0)
                            {
                                iCameraTrigger_Step = 10;  //并回到等待下一个产品触发感测器
                            }
                            //CameraTriggerThread02.iCameraTrigger_Step = 10;
                            break;
                        }
                    case 60:
                        {
                            break;
                        }
                    case 65:
                        {
                            break;
                        }
                    case 70:
                        {
                            break;
                        }
                    case 75:
                        {
                            break;
                        }
                    case 80:
                        {
                            break;
                        }
                }
                Thread.Sleep(1);
            }
        }

        private static void ComparePoints()
        {
            ushort usAxis = 0;
            ushort enable = 0;
            ushort cmp_source = 0;
            //配置比较器
            LTDMC.dmc_compare_set_config(DMC300_Info.usCardId[0], usAxis, enable, cmp_source);
        }
    }
}
